<?php 
if(isset($_POST['search'])){
	include("dbconfig.php");
	include('header.php');

	$wording= $_POST['search'];	
?>
	<div class="prl-container">
		<div class="prl-grid">
			<section class="prl-span-8">
				<div class="prl-article w-box prl-panel">
					<div class="w-box-inner">
						<h4 class="prl-category-title">ผลลัพธ์การค้นหา "<?php echo $wording?>"</h4>
					</div>
				</div>
				<?php 
				$sql = "SELECT * FROM article WHERE fix_status = 1 AND title LIKE \"%".$wording."%\" OR description LIKE \"%".$wording."%\" OR reference_st LIKE \"%".$wording."%\" ORDER BY reader_count DESC";

				$rsd = mysql_query($sql);
				$count = mysql_num_rows($rsd);
				while($row = mysql_fetch_array($rsd))
				{					
					$type = $row["fix_article_type"];
					$title = "";
					if($type==1)
						$title = "วิเคราะห์และรายงาน";
					else if($type==4)
						$title = "ปาตานีคาเฟ่";
					else if($type==5)
						$title = "บรรณาธิการ";
					else if($type==6)
						$title = "Quote";
					else if($type==7)
						$title = "ภาคสนามปาตานี";
					else if($type==8)
						$title = "การ์ตูน";
					else if($type==9)
						$title = "สื่อสิ่งพิมพ์";
					else if($type==10)
						$title = "Tupat Tube";
					else if($type==11)
						$title = "status of the day";
					else if($type==12)
						$title = "ประชาสัมพันธ์";

					$date = new DateTime($row['inserted_date_time']);
			?>
					<article class="prl-article w-box prl-panel">
						<div class="w-box-inner">

						<?php 
							if((bool)preg_match('#<img[^>]+src=[\'"]([^\'"]+)[\'"]#', html_entity_decode($row['description']), $matches))
							{
							   $imageFound = $matches[0];
							   $src = substr($imageFound, strpos($imageFound, "src"));
						?>
								<a class="thumbnail popup-link-image" href=<?php echo substr($src,4,strlen($src))?>>						
									<span class="prl-overlay">
										 <img <?php echo $src;?> alt="Patani Forum"/ >
										<span class="prl-overlay-area o-photo"></span>
									</span>
								</a>
						<?php
							}else if((bool)preg_match('#<iframe[^>]+src=[\'"]([^\'"]+)[\'"]#', html_entity_decode($row['description']), $matches))
							{
								$youtubeFound = $matches[0];
								$src = substr($youtubeFound, strpos($youtubeFound, "src"));
						?>
								<iframe <?php echo $src;?> width="500" height="281"></iframe>
						<?php
							}
						?>
							

							<h3><a href="single.php?id=<?php echo $row["article_id"]?>" target="_blank"><?php echo $row["title"]?></a> <span class="prl-badge red"><?php echo $title;?></span></h3> 
							<div class="prl-article-meta"><span><i class="fa fa-clock-o"></i> <?php echo $date->format('d/m/Y H:i:s');?></span></div>
							<p><?php echo mb_substr(strip_tags(html_entity_decode($row['description'])),0,200,'UTF-8');?></p>
							</div>	
							<div class="prl-article-bottom clearfix">
								<a href="single.php?id=<?php echo $row["article_id"]?>" target="_blank" class="prl-button light floatright">เพิ่มเติม</a>
							</div>
					</article>				
				<?php
				}
				?>				
			</section>
			<aside id="sidebar" class="prl-span-4">
				<?php include ('sidebar.php');?>
			</aside>
		</div><!--.prl-grid -->
	</div><!-- .container-->

<?php 
	include ('footer.php');
}else{
	header("Location: http://www.pataniforum.com");
}?>